package com.ymatou.iapi.selleractivity.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class ApplyActivityCall extends YmatouAutoTestCall{
	private static String opurl ="/api/ActivityProduct/ApplyActivity";
	
	public ApplyActivityCall() {
		super(EnvSetup.getSystemUrl("selleractivity"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	public static void main(String[] args) throws Exception {
	
		
	}
	
	
	/***
	 * 报名商品下所有规格商品
	 * @param sellerId
	 * @param productId
	 * @param activityId
	 * @param updateMap
	 * @return
	 * @throws Exception
	 */
	public static JSONObject applyProductInActivity(int sellerId,String productId,int activityId,Map<String,Object> updateMap)throws Exception {
		return applyProductInActivity(sellerId,productId,activityId,null,MapUtil.hashMap());
	}
	
	
	
	/***
	 * 根据给出规格id list报名参加活动
	 * @param sellerId
	 * @param productId
	 * @param activityId
	 * @param updateMap
	 * @return
	 * @throws Exception
	 */
	public static JSONObject applyProductInActivity(int sellerId,String productId,int activityId,List<String> catalogList,Map<String,Object> updateMap) throws Exception{
		//updateMap do nothing
		ApplyActivityCall activityCall = new ApplyActivityCall();
		JSONObject applyEffectiveActBean = activityCall.getJsonBean(null);
		
		//指定catalog list
		List<YmtCatalogsI> ymtCatalogsIList = new ArrayList<>();
		YmtCatalogsIWapper ymtCatalogsIWapper=new YmtCatalogsIWapper();
		
		if (catalogList==null || catalogList.size()==0){
			ymtCatalogsIList.addAll(ymtCatalogsIWapper.selectCatalogsByProductId(productId));
		}else{
			catalogList.forEach(ele->{
				try {
					ymtCatalogsIList.add(ymtCatalogsIWapper.selectCatalogsByCatalogId(ele));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			});
		}
		
		
		if (ymtCatalogsIList.size()>0){
			Map<String, Object> requestMap = new HashMap<String, Object>();
			requestMap.put("SellerId", sellerId);
			requestMap.put("ActivityId", activityId);
			requestMap.put("ActivityProducts[0]->ProductId",productId);
			
			requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->CatalogId",ymtCatalogsIList.get(0).getsCatalogId());
			requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ActivityStockNum",5);
			requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ActivityPrice",ymtCatalogsIList.get(0).getfQuotePrice().doubleValue()*0.5);
			requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->Sku",null);
			requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->BkcKey",null);
			requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ProductInActivityId",0);
			
			JsonBeanHelper.updateJsonBean(applyEffectiveActBean, requestMap);
			
			for(int i=1;i<ymtCatalogsIList.size();i++){
				JSONArray CatalogListBean = applyEffectiveActBean.getJSONArray("ActivityProducts").getJSONObject(0).getJSONArray("ApplyCatalogs");
				CatalogListBean.put(i, new JSONObject(CatalogListBean.get(0).toString()));
				
				
				
				CatalogListBean.getJSONObject(i).put("CatalogId",ymtCatalogsIList.get(i).getsCatalogId());
				CatalogListBean.getJSONObject(i).put("ActivityStockNum",5);
				CatalogListBean.getJSONObject(i).put("ActivityPrice",ymtCatalogsIList.get(i).getfQuotePrice().doubleValue()*0.5);
				CatalogListBean.getJSONObject(i).put("Sku","");
				CatalogListBean.getJSONObject(i).put("BkcKey","");
				CatalogListBean.getJSONObject(i).put("ProductInActivityId",0);
			}
		}
		activityCall.setData(applyEffectiveActBean);
		activityCall.callService();
		return new JSONObject(activityCall.getReturnData());
	}
	
	
	
	
}
